package com.caowj.composedemo.activity

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.AccountCircle
import androidx.compose.material.icons.outlined.Home
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavController
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.caowj.composedemo.R
import com.caowj.composedemo.ui.theme.ComposeDemoTheme

/**
 * Navigation之BottomNavigation
 * 底部导航栏
 * https://blog.csdn.net/weixin_42404361/article/details/122096569
 * https://blog.csdn.net/Mr_Tony/article/details/118971169
 */
class BottomNavigationActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeDemoTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    compose_16()
                }
            }
        }
    }
}

/**
 * 底部导航栏
 */
@Composable
fun compose_16() {
    val navController = rememberNavController()//实例化控制器
    Scaffold(
        bottomBar = {
            BottomNavigation(backgroundColor = Color.White) {
                //返回back堆栈的顶部条目
                val navBackStackEntry by navController.currentBackStackEntryAsState()
                //返回当前目标
                val currentDestination = navBackStackEntry?.destination
                //返回当前route
//                val currentRoute = navBackStackEntry?.destination?.route ?: Screen.Home.route
                items.forEach { screen: Screen ->
                    BottomNavigationItem(
                        icon = {
                            Icon(
                                imageVector = screen.icon,
                                contentDescription = null
                            )
                        },
                        label = { Text(text = stringResource(screen.resourceId)) },
                        selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
                        //选中选项的颜色 (text\icon\波纹)
                        selectedContentColor = Color.Red,
                        //未选中选项的颜色 (text\icon\波纹)
                        unselectedContentColor = Color.Gray,
                        onClick = {
//                            //判断是否是当前的route,如果是就不做处理
//                            if (bottomNavScreenItem == bottomNavScreen) {
//                                return@BottomNavigationItem
//                            }

                            navController.navigate(screen.route) {
                                // Pop up to the start destination of the graph to
                                // avoid building up a large stack of destinations
                                // on the back stack as users select items
                                popUpTo(navController.graph.findStartDestination().id) {
                                    saveState = false
                                }
                                // Avoid multiple copies of the same destination when
                                // reselecting the same item
                                launchSingleTop = true
                                // Restore state when reselecting a previously selected item
                                restoreState = false
                            }
                        }
                    )
                }
            }
        }
    ) { innerPadding ->
        NavHost(//路由跳转
            navController,
            startDestination = Screen.Home.route,//导航开始页面
            Modifier.padding(innerPadding)
        ) {
            composable(Screen.Home.route) { _Home(navController) }
            composable(Screen.Profile.route) { _Mine(navController) }
        }
    }
}

@Composable
fun _Home(navController: NavController) {
    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        Text(text = "这是首页")
    }
}

@Composable
fun _Mine(navController: NavController) {
    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        Text(text = "这是个人中心")
    }
}


/**
 * 定义密封类
 */
sealed class Screen(val route: String, @StringRes val resourceId: Int, val icon: ImageVector) {
    object Home : Screen("home", R.string.home, Icons.Outlined.Home)
    object Profile : Screen("profile", R.string.profile, Icons.Outlined.AccountCircle)
}

/**
 * 将定义的项放到BottomNavigationItem可使用的列表中
 */
val items = listOf(
    Screen.Home,
    Screen.Profile,
)